package com.yunji.web.storage;

import com.github.pagehelper.page.PageMethod;
import com.yunji.aggregate.model.dto.stock.AllocateTicketParam;
import com.yunji.aggregate.model.vo.stock.AllocateTicketStatisticsVO;
import com.yunji.aggregate.service.StockAggregateService;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.utils.ExcelUtil;
import com.yunji.storage.business.AllocateTicketBusiness;
import com.yunji.storage.dto.AllocateTicketDTO;
import com.yunji.storage.vo.AllocateTicketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * @Project: yunji-coffee
 * @Package: com.yunji.web.storage
 * @Title: AllocateTicketController
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-08-20 15:04:29      V1.0        HuaAo       新建类
 */
@Api(tags = "管理端-调拨模块")
@RestController
@RequestMapping("storage/allocate")
public class AllocateTicketController extends BaseController {

    @Resource
    AllocateTicketBusiness business;

    @Resource
    StockAggregateService aggregateService;

    /**
     * @param param
     * @Title export
     * @Desc 导出调拨单数据
     * @Date 2024-10-10 14:45:03.709
     */
    @ApiOperation("导出调拨单")
//    @PreAuthorize("@ss.hasPermi('inventory:transfer:export')")
    @PostMapping("/export")
    public void export(AllocateTicketParam param) {
        List<AllocateTicketStatisticsVO> list = aggregateService.searchAllocateList(param);
        ExcelUtil<AllocateTicketStatisticsVO> util = new ExcelUtil<>(AllocateTicketStatisticsVO.class);
        util.exportExcel(response, list, "调拨单数据");
    }

    /**
     * 查询调拨单列表
     *
     * @param param
     * @return
     */
    @ApiOperation("查询调拨单列表")
    @PostMapping("/list")
    public Response<List<AllocateTicketStatisticsVO>> list(@RequestBody @Valid AllocateTicketParam param) {
        PageMethod.startPage(param.getPageNum(), param.getPageSize());
        List<AllocateTicketStatisticsVO> list = aggregateService.searchAllocateList(param);
        return ResponseHelper.successWithPage(list);
    }

    /**
     * 获取调拨单详情
     *
     * @param allocateId
     * @return
     */
    @ApiOperation("获取调拨单详情")
    @GetMapping("/{allocateId}")
    public Response<AllocateTicketVO> getInfo(@PathVariable Long allocateId) {
        return ResponseHelper.success(business.select(allocateId));
    }

    /**
     * 新增调拨单
     *
     * @param dto
     * @return
     */
    @ApiOperation("新增调拨单")
//    @PreAuthorize("@ss.hasPermi('inventory:transfer:add')")
    @PostMapping
    public Response<Long> create(@RequestBody AllocateTicketDTO dto) {
        return ResponseHelper.success(business.create(dto));
    }

    /**
     * 取消调拨单
     *
     * @param allocateId
     * @return
     */
    @ApiOperation("取消调拨单")
//    @PreAuthorize("@ss.hasPermi('inventory:transfer:cancel')")
    @PutMapping("/cancel")
    public Response<String> cancel(@RequestParam Long allocateId) {
        business.cancel(allocateId);
        return ResponseHelper.success("取消成功");
    }

    /**
     * 重新提交调拨单
     *
     * @param dto
     * @return
     */
    @ApiOperation("重新提交调拨单")
//    @PreAuthorize("@ss.hasPermi('inventory:transfer:update')")
    @PostMapping("/commit")
    public Response<String> commit(@RequestBody AllocateTicketDTO dto) {
        business.commit(dto);
        return ResponseHelper.success("提交成功");
    }

    /**
     * 查询出入库记录
     *
     * @param
     * @return
     */
    @ApiOperation("保存草稿")
    @PostMapping("/saveDraft")
    public Response<String> saveDraft(@RequestBody AllocateTicketDTO dto) {
        business.saveDraft(dto);
        return ResponseHelper.success("保存成功");
    }


    /**
     * 获取草稿
     *
     * @param belongId
     * @return
     */
    @ApiOperation("获取草稿")
    @GetMapping("/getDraft")
    public Response<AllocateTicketVO> getDraft(@RequestParam Long belongId) {
        return ResponseHelper.success(business.getDraft(belongId));
    }

    /**
     * 删除草稿
     *
     * @param allocateIds
     * @return
     */
    @ApiOperation("/删除调拨单")
//    @DeleteMapping("/{allocateIds}")
    @PreAuthorize("@ss.hasPermi('inventory:transfer:remove')")
    public Response<String> delete(@PathVariable Long... allocateIds) {
        business.delete(allocateIds);
        return ResponseHelper.success("删除成功");
    }

}